<style lang="less">
@import "../../../assets/style/yhqx_detail.less";
</style>

<template>
	<mxq-router-layout>
		<div class="detail yinhuan_detail">
			<Head slot="header" @headerClick="headerClick($event)">
				<div slot="headerTitle">
					<span>隐患详情</span>
				</div>
				<div slot="headerRight">
					<i class="iconfont icon-shouye"/>
					<!--<van-icon class="home" name="home"/>-->
				</div>
			</Head>
			<detailInfo :data="formData"></detailInfo>
			
			<van-cell-group class="mt10" v-show = "delayShow">
				<van-field
					class="detail_item"
					clearable
					label="隐患处理方式"
					required>
					<van-icon slot="icon" class="slot-cont">
						<span class="left">
							<label for="sfcl"  class="check_label">隐患核查</label>
							<van-switch :disabled="!($route.query.defectType==-1&&$route.query.towerStatus==1)"   v-model="defectType1" size="20px" @change="changeDefect($event,'defectType2')"/>
						</span>
						<span class="right">
							<label for="sfcl" class="check_label">隐患消除</label>
							<van-switch :disabled="!($route.query.defectType==-1&&$route.query.towerStatus==1)"   v-model="defectType2" class="chk_r" size="20px" @change="changeDefect($event,'defectType1')"/>
						</span>
					</van-icon>
				</van-field>
			</van-cell-group>
			<van-cell-group class="mt10" v-if="defectType2 || defectType1" v-show = "delayShow">
				<task-photo
				:photoLists="photoLists"
				:photoImg="photoImg"
				:showHide="showHide"
				@addPhotos="addPhotos"
				@deletePhotos="deletePhotos"
				/>
			</van-cell-group>
			
			<van-button v-if="$route.query.defectType==-1&&$route.query.towerStatus==1" v-show = "delayShow" size="large" type="primary" class="upload" @click="sureBtn">确定</van-button>
			
		</div>
	</mxq-router-layout>
</template>

<script>
import { callData,fileData,all,fileUrl } from '../../../axios/axios'
import Head from '@/components/head';
import detailInfo from '@/components/detailInfo.vue';
import photoImg from '../../../assets/img/changguixunshi/icon_allow.png';
import TaskPhoto from '../../components/yinhuan-photo.vue'
import {
	Row,
    Col,
    Cell,
    CellGroup,
	Field,
	Button,
	Icon,
	Switch,
	Toast,
	Dialog
} from 'vant';
export default {
    name: 'yinhuanDetail',
    components: {
		Head,
		TaskPhoto,
		detailInfo,
		[Row.name]:Row,
		[Col.name]:Col,
        [CellGroup.name]: CellGroup,
        [CellGroup.name]: CellGroup,
		[Field.name]: Field,
		[Button.name]: Button,
		[Icon.name]: Icon,
		[Switch.name]: Switch,
		[Toast.name]: Toast,
		[Dialog.name]: Dialog
	},
	data() {
		return {
			showHide: true,				//用于任务已完成状态隐藏拍照按钮
			qxDetail:[],
			photoImg: photoImg,
			delayShow: false,
			checked1: true,
			checked2: false,
			data: {},
			file: '',
			dataYhId:'',//确定按钮需要的id
			formData:[],
			defectType1:true,
			defectType2:false,
			pictureType: 1,// 1 核查，0消缺
			photoLists: [
                {
                    title: "隐患处理照片",
                    photo:[]
                }
            ],
		}
	},
	
    methods: {// 头部两侧按钮点击事件
		headerClick(e){
			let query = {}
			this.$router.push({
				name:'index',
				query: query
			})
		},
		
		homeShow(data){
			var ls_picture = data.ls_picture;//图片
			var data = data.yh[0];//数据
			var yinhuanIndex = this.$route.query.yhLb;
			const levelMap = {0:'正常', 1:'关注', 2:'严重'}//风险等级
			const statusMap = {0:'未处理', 1:'已受控', 2:'已闭环'}//当前状态
			const isMachineryMap = {1:'有',0:'无'}//有无大型机械
			const yhConstructionScheduleMap = {0:'未开工', 1:'已开工', 2:'停工中',3:'已完工'}//施工进度
			//这里根据传进来的参数不一样，从而判断显示哪个页面
        	if(yinhuanIndex == '503988636121874432'){	//树障
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '树种',value: data.EJLB},
						{label: '数量',value: data.YH_NUMBER},
						{type:	'area',label: '隐患描述',value: data.YH_DESCRIPTION},
						{label: '胸径（厘米）',value: data.DBH},
						{label: '树高（米）',value: data.YH_HIGH},
						{label: '净空（米）',value: data.YH_CLEARANCE},
						{label: '业主单位',value: data.YH_OWNERUNIT},
						{label: '业主联系人',value: data.YH_OWNERUSER},
						{label: '联系电话',value: data.YH_OWNERPHONE},
						{label: '来源',value: data.YH_SOURCE},
						{label: '当前状态',value: statusMap[data.YH_STATUS]},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}else if(yinhuanIndex == '503989129904701440'){//违章建筑物
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '建筑类别',value: data.EJLB},
						{label: '数量(处)',value: data.YH_NUMBER},
						{type:	'area',label: '隐患描述',value: data.YH_DESCRIPTION},
						{label: '长（米）',value: data.YH_LONG},
						{label: '宽（米）',value: data.YH_WIDTH},
						{label: '高（米）',value: data.YH_HIGH},
						{label: '净空（米）',value: data.YH_CLEARANCE},
						{label: '业主单位',value: data.YH_OWNERUNIT},
						{label: '业主联系人',value: data.YH_OWNERUSER},
						{label: '联系电话',value: data.YH_OWNERPHONE},
						{label: '来源',value: data.YH_SOURCE},
						{label: '当前状态',value: statusMap[data.YH_STATUS]},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}else if(yinhuanIndex == '503989282900328448'){	//交叉跨越隐患
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '跨越物类别',value: data.EJLB},
						{label: '数量(处)',value: data.YH_NUMBER},
						{type:	'area',label: '隐患描述',value: data.YH_DESCRIPTION},
						{label: '高（米）',value: data.YH_HIGH},
						{label: '净空（米）',value: data.YH_CLEARANCE},
						{label: '业主单位',value: data.YH_OWNERUNIT},
						{label: '业主联系人',value: data.YH_OWNERUSER},
						{label: '联系电话',value: data.YH_OWNERPHONE},
						{label: '来源',value: data.YH_SOURCE},
						{label: '当前状态',value: statusMap[data.YH_STATUS]},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}else if(yinhuanIndex == '503989531907768320'){	// 异物隐患
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '异物类别',value: data.EJLB},
						{label: '数量(处)',value: data.YH_NUMBER},
						{type:	'area',label: '隐患描述',value: data.YH_DESCRIPTION},
						{label: '业主单位',value: data.YH_OWNERUNIT},
						{label: '业主联系人',value: data.YH_OWNERUSER},
						{label: '联系电话',value: data.YH_OWNERPHONE},
						{label: '来源',value: data.YH_SOURCE},
						{label: '当前状态',value: statusMap[data.YH_STATUS]},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture}
					]
        	}else if(yinhuanIndex == '503989671221575680'){	//施工外破
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '施工类型',value: data.EJLB},
						{label: '数量(处)',value: data.YH_NUMBER},
						{type:	'area',label: '隐患描述',value: data.YH_DESCRIPTION},
						{label: '项目名称',value: data.YH_PROJECT_NAME},
						{label: '施工单位',value: data.YH_CONSTRUCTION_UNIT},
						{label: '施工联系人',value: data.YH_CONSTRUCTION_USER},
						{label: '施工联系人电话',value: data.YH_CONSTRUCTION_PHONE},
						{label: '净空（米）',value: data.YH_CLEARANCE},
						{label: '业主单位',value: data.YH_OWNERUNIT},
						{label: '业主联系人',value: data.YH_OWNERUSER},
						{label: '业主联系人电话',value: data.YH_OWNERPHONE},
						{label: '施工进度',value: yhConstructionScheduleMap[data.YH_CONSTRUCTION_SCHEDULE]},
						{label: '当前状态',value: statusMap[data.YH_STATUS]},
						{label: '有无使用大型机械',value: isMachineryMap[data.YH_IS_MACHINERY]},
						{label: '预计完工日期',value: data.YH_EXPECTED_DATE},
						{label: '水平距离（米）',value: data.YH_HORIZONTAL_DISTANCE},
						{label: '施工大型机械名称',value: data.YH_MACHINERY_NAME},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}else if(yinhuanIndex == '503989986297692160'){	//消防隐患
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '火灾来源',value: data.EJLB},
						{label: '数量(处)',value: data.YH_NUMBER},
						{type:	'area',label: '隐患描述',value: data.YH_DESCRIPTION},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}else if(yinhuanIndex == '503990076341010432'){	//	防汛隐患
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '二级类别',value: data.EJLB},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}else {	//其他隐患503990222193737728
        		this.formData = [{label: '线路名称',value: data.LINE_NAME}, 
						{label: '运行塔号',value: data.TOWER_NAME}, 
						{label: '隐患位置',value: data.QX_POSITION},
						{label: '隐患类别',value: data.YHLB},
						{label: '风险等级',value: levelMap[data.YH_RISK_LEVEL]},
						{type:	'area',label: '风险描述',value: data.YH_RISK_DESCRIPTION},
						{label: '当前措施',value: data.YH_CURRENT_MEASURES},
						{type: 'images',label: '隐患照片',value: ls_picture},
					]
        	}
		},
		getDetail(){
			var params = {
        		yhId:this.$route.query.yhId,
        		towerTaskId:this.$route.query.towerTaskId
        	}
			const _this = this
        	callData('/accountServer/CmYhLb/appFindYhByID', 'get', params).then(data => {
				var data = data.data;
				_this.dataYhId = data.yh[0].ID;
				_this.homeShow(data)
				//处理过隐患之后的回显照片
				data.picture.forEach(v => {
					_this.photoLists[0].photo.push(fileUrl(v.FILE_PATH))
				})
				_this.delayShow = true
			})
		},
		//确定按钮
		sureBtn(){
			let defectType = -1;
			if (!this.defectType1 && !this.defectType2) {
				Toast('请先对隐患进行操作！')
				return
			}
			if(this.defectType1) {
				defectType = 0
				this.pictureType = 1
			}else if (this.defectType2) {
				defectType = 1
				this.pictureType = 0
			}
			if( this.file.file.length == 0 ){
				Dialog.alert({
					title: '提示',
					message: '请拍照！'
				}).then(() => {
					
				});
				return false;
			}
			var params ={
				id:this.dataYhId,
				defectType: defectType,//操作类型0 复核，1消缺
				towerTaskId:this.$route.query.towerTaskId,
        			taskType:1,//任务类型 0 常规巡视 1 特殊巡视 2 保电值守 3 线路检修
				taskId:this.$route.query.taskId,
				lineTowerId:this.$route.query.lineTowerId,
				towerId:this.$route.query.towerId,
				lineId:this.$route.query.lineId,
				defectStatus:1//0新增,1历史,此时是对历史操作1
			}
			const _this = this
			const toast = Toast.loading({
			  duration: 0,       // 持续展示 toast
			  forbidClick: true, // 禁用背景点击
			  mask: true,
			  loadingType: 'spinner',
			  message: '上传中...'
			});
        	callData('/accountServer/CmYhLb/appAddYh', 'post', params).then(data => {
        		if (data.success) {//上传数据成功之后上传照片
					_this.uploadPhoto(data)
				}
			})
		},
		addPhotos(file){
			this.file = file
    	},
    	deletePhotos(index, type) {
    		this.file.file.splice(index, 1)
    	},
    	uploadPhoto(data) {
    		const _this = this
			let allConfig = []
			const files = this.file
			if(files.file &&　files.file.length>0) {
				// 此处用axios.all发送异步请求
				for(var i=0;i < files.file.length; i++){
	    			if(window.Cordova){
	    				let param = {}
	    				let filePath = files.file[i]
						param.taskId = this.$route.query.taskId
						param.towerTaskId = this.$route.query.towerTaskId
						param.towerId = this.$route.query.towerId
						param.lineTowerId = this.$route.query.lineTowerId
						param.yhId = data.data.yhId
						param.yhReviewId = data.data.yhReviewId
						param.jd = files.jdwd[i].lon
						param.wd = files.jdwd[i].lat
						param.pictureType = _this.pictureType
						allConfig.push(fileData('/fileserver/CmYhPicture/saveCmYhPicture', 'post', param,filePath))
	    			}else{
	    				let param = new FormData()
	    				param.append('file',files.file[i])
						param.append('taskId',this.$route.query.taskId)
						param.append('towerTaskId',this.$route.query.towerTaskId)
						param.append('towerId',this.$route.query.towerId)
						param.append('lineTowerId',this.$route.query.lineTowerId)
						param.append('yhId',data.data.yhId)
						param.append('yhReviewId',data.data.yhReviewId)
						param.append('jd',files.jdwd[i].lon)
						param.append('wd',files.jdwd[i].lon)
						param.append('pictureType',_this.pictureType)
						allConfig.push(fileData('/fileserver/CmYhPicture/saveCmYhPicture', 'post', param))
	    			}
	    		}
				all(allConfig,(data)=>{
					//返回上一页
					//_this.back()
					Toast.clear();
					if(data[0].success){
						Toast("操作成功！")
						this.$router.go(-1)
					} else {
						Toast("操作失败！")
					}
				})
			}else {
				// 没有照片直接返回
				//_this.back()
				Toast("操作成功！")
				this.$router.go(-1)
			}
    	},
    	changeDefect(value, type) {
    		if (value) {
				this[type] = false 
			} else {
				this[type] = true
			}
    	},
    	back(){
    		//成功之后返回页面
    		let query = {
    			towerId:this.$route.query.towerId,
				towerTaskId:this.$route.query.towerTaskId,
				taskId :this.$route.query.taskId,
				lineId:this.$route.query.lineId,
				lineTowerId :this.$route.query.lineTowerId
    		}
			this.$router.push({
				name:'tsxs_yinhuanList',
				query: query
			})
    	}
	},
	mounted(){
		this.getDetail();
		//0是复核，1是消缺，2和-1代表没有
		var defectType = this.$route.query.defectType
		if(defectType == 0){//复核
			this.defectType1 = true
			this.defectType2 = false
		}else if(defectType == 1){//消缺
			this.defectType1 = false
			this.defectType2 = true
		}else{
			this.defectType2 = false
			this.defectType1 = true
		}
		if(!(this.$route.query.defectType==-1&&this.$route.query.towerStatus==1)){
			this.showHide = false;
		}
	}
}
</script>